package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.entity.Cart;
import com.yc.mapper.CartMapper;
import com.yc.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * InnoDB free: 5120 kB; (`pid`) REFER `test/product`(`pid`) ON DELETE NO ACTION 前端控制器
 * </p>
 *
 * @author ly
 * @since 2022-05-08
 */
@Controller
@RequestMapping("//cart")
public class CartController {
    @Resource
    private CartMapper cartMapper;
    //添加购物车
    @RequestMapping("add")
    @ResponseBody
    public Result add(@RequestBody Cart cart){
        //TODO 如果商品存在，要更新数量
        if ( cartMapper.updateCount(cart)==0 ){
            cartMapper.insert(cart);
        }
        return new Result(1,"添加购物车成功",cart);
    }

    //查询购物车
    @RequestMapping("query")
    @ResponseBody
    public List<Cart> query(int uid){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("uid",uid);
        List<Cart> list=cartMapper.selectList(queryWrapper);
        return list;
    }

    @RequestMapping("queryByUid")
    @ResponseBody
    public List<Cart> queryByUid(int uid){
        return cartMapper.selectByUid(uid);
    }
}

